package com.edu.chongdian;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

public class MarkerDialog extends Dialog {
    public MarkerDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        //设置位置在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置为全屏dialog
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    //地图充电桩点击弹出来的dialog
    public MarkerDialog(@NonNull Context context) {
        super(context);
    }
    public static class Builder{
        private TextView address,xiangXiAddress,yueKa,yuE;
        private Button daoHang;
        private MarkerDialog dialog;
        private View mLayout;
        private View.OnClickListener yueKaOnClickListener,yuEOnClickListener,daoHangOnClickListener;
        Context context;
        public Builder (Context context){
            this.context=context;
            dialog =new MarkerDialog(context,R.style.maker_dialog);
            LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mLayout=inflater.inflate(R.layout.marker_dialog_layout,null,false);
            dialog.addContentView(mLayout,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
            address=mLayout.findViewById(R.id.address_m);
            xiangXiAddress=mLayout.findViewById(R.id.xiang_xi_address);
            yueKa=mLayout.findViewById(R.id.yue_ka);
            yuE=mLayout.findViewById(R.id.yu_e);
            daoHang=mLayout.findViewById(R.id.dao_hang);
        }

        public void setAddress(String address) {
            this.address.setText(address);
        }

        public void setXiangXiAddress(String xiangXiAddress) {
            this.xiangXiAddress .setText(xiangXiAddress);
        }

        public void setYueKaOnClickListener(View.OnClickListener yueKaOnClickListener) {
            this.yueKaOnClickListener = yueKaOnClickListener;
        }

        public void setYuEOnClickListener(View.OnClickListener yuEOnClickListener) {
            this.yuEOnClickListener = yuEOnClickListener;
        }

        public void setDaoHangOnClickListener(View.OnClickListener daoHangOnClickListener) {
            this.daoHangOnClickListener = daoHangOnClickListener;
        }

        public MarkerDialog create(){
            yueKa.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Drawable drawable = context.getResources().getDrawable(R.drawable.shape_yuan_1);
                    Drawable drawable1 = context.getResources().getDrawable(R.drawable.shape_yuan);
                    yueKa.setBackground(drawable);
                    yuE.setBackground(drawable1);
                    yueKaOnClickListener.onClick(v);
                }
            });
            yuE.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Drawable drawable = context.getResources().getDrawable(R.drawable.shape_yuan_1);
                    Drawable drawable1 = context.getResources().getDrawable(R.drawable.shape_yuan);
                    yuE.setBackground(drawable);
                    yueKa.setBackground(drawable1);
                    yuEOnClickListener.onClick(v);
                }
            });
            daoHang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    daoHangOnClickListener.onClick(v);
                    dialog.dismiss();
                }
            });
            //设置布局
            dialog.setContentView(mLayout);
            //设置是否可取消
            dialog.setCancelable(true);
            //点击空白关闭
            dialog.setCanceledOnTouchOutside(true);
            return dialog;
        }
    }
}
